<?php

namespace Registro\Plugins;

use Phalcon\Events\Event;
use Phalcon\Dispatcher;
use Phalcon\Mvc\User\Plugin;
use Phalcon\Mvc\Dispatcher\Exception as DispatcherException;
use Phalcon\Mvc\Dispatcher as MvcDispatcher;

/**
 * NotFoundPlugin
 *
 * Handles not-found controller/actions
 */
class RouteNotFoundPlugin extends Plugin {

    /**
     * This action is executed before execute any action in the application
     *
     * @param Event $event
     * @param Dispatcher $dispatcher
     */
    public function beforeException( Event $event, MvcDispatcher $dispatcher, \Exception $exception )
    {

        if( $exception instanceof DispatcherException )
        {
            switch( $exception->getCode() ){
                case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward( array(
                        'namespace' => 'Registro\Common\Controllers',
                        'controller' => 'exceptions',
                        'action' => 'show404',
                        'params' => array( 'message' => $event->getData())
                    ) );
                    return false;
            }
        }

        $dispatcher->forward( array(
            'namespace' => 'Registro\Common\Controllers',
            'controller' => 'exceptions',
            'action' => 'show500',
            'params' => array( 'message' => $exception->getMessage() )
        ) );
        return false;
    }
}